package polyu.comp;


import android.app.*;
import android.content.*;
import android.location.*;
import android.os.*;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MyActivity extends Activity {

	TextView tvState;
	Asensor mAsensor;
	long startRunTime = 0;
	String lastState = "";
	LocationManager mLocationManager;
	Location mLocation;
	
	
	private LocationListener mLocationListener = new LocationListener() {
		public void onLocationChanged(Location location) {
		}

		public void onProviderDisabled(String provider) {
		}

		public void onProviderEnabled(String provider) {
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	};
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my);

		tvState = (TextView) findViewById(R.id.tvState);// Button Start

		Button btnReport = (Button) findViewById(R.id.btnReport);
		btnReport.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				
			}
		});
		
		
		
	
		mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		
		mAsensor = new Asensor(this, new Handler() {
			public void handleMessage(Message msg) {
				
				float tmpt;
				float tmpx;
				float tmpy;
				float tmpz;
				String Label = "";
				
				tmpx = msg.getData().getFloat(Asensor.TAG_DX);
				tmpy = msg.getData().getFloat(Asensor.TAG_DY);
				tmpz = msg.getData().getFloat(Asensor.TAG_DZ);
				tmpt = tmpx * tmpx + tmpy * tmpy + tmpz * tmpz;
				
				if (tmpy <= -3.333268 && tmpx <= 4.292474)
					Label = "Running";
				else if (tmpy <= -3.333268 && tmpx > 4.292474
						&& tmpz <= -3.411107 && tmpx <= 7.166497)
					Label = "Running";
				else if (tmpy <= -3.333268 && tmpz <= -3.411107
						&& tmpx > 7.166497)
					Label = "Walking";
				else if (tmpy <= -3.333268 && tmpx > 4.292474
						&& tmpz > -3.411107)
					Label = "Walking";
				else if (tmpy <= -3.333268 && tmpx > 4.292474
						&& tmpz > -1.035247 && tmpt <= 88.97408)
					Label = "Walking";
				else if (tmpy <= -3.333268 && tmpx > 4.292474
						&& tmpz > -1.035247 && tmpt > 88.97408)
					Label = "Running";
				else if (tmpy > -3.333268 && tmpx <= 3.564388
						&& tmpt <= 105.30124 && tmpy <= 9.504036
						&& tmpz <= 4.482878)
					Label = "Running";
				else if (tmpy > -3.333268 && tmpx <= 3.564388
						&& tmpt <= 105.30124 && tmpy <= 9.504036
						&& tmpz > 4.482878)
					Label = "Stationary";
				else if (tmpy > -3.333268 && tmpx <= 3.564388
						&& tmpt <= 105.30124 && tmpy > 9.504036)
					Label = "Stationary";
				else if (tmpy > -3.333268 && tmpx <= 3.564388
						&& tmpt > 105.30124)
					Label = "Running";
				else if (tmpy > -3.333268 && tmpx > 3.564388)
					Label = "Running";
				
				/*

				if (tmpz < 8.83223 && tmpy < -3.04587 && tmpx < 4.36911)
					Label = "Running";
				else if (tmpz < 8.83223 && tmpy < -3.04587 && tmpz < -1.03525)
					Label = "Walking";
				else if (tmpz < 8.83223 && tmpy < -3.04587 && tmpz >= -1.03525
						&& tmpt < 90.55089)
					Label = "Walking";
				else if (tmpz < 8.83223 && tmpy < -3.04587 && tmpz >= -1.03525
						&& tmpt > 90.55089)
					Label = "Running";
				else if (tmpz < 8.83223 && tmpy >= -3.04587)
					Label = "Running";
				else if (tmpz >= 8.83223 && tmpx < 9.44656)
					Label = "Stationary";
				else if (tmpz >= 8.83223 && tmpx >= 9.44656)
					Label = "Running";
				
				 * z < 8.83223 | y < -3.04587 | | x < 4.36911: Running(8.0/0.0)
				 * | | x >= 4.36911 | | | z < -1.03525 | | | | z < -3.37279:
				 * Walking(6.0/3.0) | | | | z >= -3.37279: Walking(18.0/0.0) | |
				 * | z >= -1.03525 | | | | f < 90.55089: Walking(2.0/1.0) | | |
				 * | f >= 90.55089: Running(3.0/0.0) | y >= -3.04587:
				 * Running(50.0/0.0) z >= 8.83223 | x < 9.44656:
				 * Sitting(52.0/0.0) | x >= 9.44656: Running(2.0/0.0)
				 */
				
				MyActivity.this.tvState.setText(Label);
				long curTime = System.currentTimeMillis();
				
				//String msgString = String.format("cur = %d, last = %d, state = %s", curTime, MyActivity.this.startRunTime,MyActivity.this.lastState);
				//Log.v(MyActivity.class.getName(), msgString);
				
				if (curTime - MyActivity.this.startRunTime > 1000 && 
						MyActivity.this.lastState.equalsIgnoreCase("Running")) {
					
					// sample GPS
					mLocation = mLocationManager
							.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
					mLocationManager.requestLocationUpdates(
							LocationManager.NETWORK_PROVIDER, 0, 0,
							mLocationListener);
					if (mLocation != null) {
						String contentTextLocation = "Latitude: "
								+ mLocation.getLatitude() + ", longitude: "
								+ mLocation.getLongitude();
						
						Log.v(MyActivity.class.getName(), contentTextLocation);
						
						// Build an ActionItem and send to server.
						ActionItem ai = new ActionItem(
								Util.uid, 
								curTime / 1000, 
								curTime / 1000, 
								curTime / 1000, 
								2);
						
						String disp;
						if (DataManager.instance().addActivity(ai) == true)
							disp = "Activity added to server.";
						else
							disp = "Request stored locally and will be send when connected.";
						
						//Toast.makeText(MyActivity.this, disp, Toast.LENGTH_LONG).show();
						
					}
					
					// reset the timer
					MyActivity.this.startRunTime = curTime;
					
				}
				
				if (!Label.equalsIgnoreCase("Running")) {
					MyActivity.this.startRunTime = curTime;
				}
				MyActivity.this.lastState = Label;
				
			}

		});
		
		// start sensor
		if (mAsensor != null) {
			mAsensor.startListening();
		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		
		if (mAsensor != null) {
			mAsensor.stopListening();
		}

	}

}
